package cn.hinglo.gameclaw.admin.action;

import cn.hinglo.gameclaw.admin.service.UserService;
import cn.hinglo.gameclaw.client.service.*;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @createTime: 2018/1/2
 * @author: HingLo
 * @description: 用户管理操作
 */
@RestController
@RequestMapping("admin/u/")
public class UserAction extends BaseController {

    /**
     * 用户信息管理的服务层对象
     */
    private final UserService userService;

    /**
     * 地址管理的服务层
     */
    private final AddressService addressService;


    /**
     * 签到记录的服务层
     */
    private final SignService signService;
    /**
     * 抓取记录
     */
    private final GrabNoteService grabNoteService;

    /**
     * 订单查看
     */
    private final OrderClientService orderClientService;

    /**
     * 邀请码处理
     */
    private final InvitationService invitationService;

    @Autowired
    public UserAction(UserService userService, AddressService addressService, SignService signService, GrabNoteService grabNoteService, OrderClientService orderClientService, InvitationService invitationService) {
        this.userService = userService;
        this.addressService = addressService;
        this.signService = signService;
        this.grabNoteService = grabNoteService;
        this.orderClientService = orderClientService;
        this.invitationService = invitationService;
    }

    /**
     * 查询用户的信息
     *
     * @param start 开始位置
     * @param num   每一大小
     * @param type  账号类型(QQ 或者weixin)
     * @return 用户列表
     */
    @GetMapping("list")
    public Result listUser(Integer type, Integer start, Integer num) {
        return userService.listUserService(type, start, num);
    }

    /**
     * 统计具体的某个类型的账号人数总数
     *
     * @param type 统计账号类型
     * @return 结果
     */
    @GetMapping("count")
    public Result countUser(Integer type) {
        return userService.countUserService(type);
    }

    /**
     * @param type 透过类型的来查询，0,1,2，这三种类型
     * @param id   查询的value
     * @return 查询的操作结果
     */
    @GetMapping("find")
    public Result findByIdOrUidUser(Integer type, String id) {
        return userService.findByIdOrUidUserService(type, id);
    }


    /**
     * 用户地址列表
     *
     * @param userId 用户的唯一ID
     * @return 用户的ID地址列表
     */
    @GetMapping("address")
    public Result listAddress(@RequestParam("userId") String userId) {
        List<Address> addressList = addressService.listAddressService(userId);
        return success(addressList);
    }

    /**
     * 通过地址的唯一Id来查询地址对象，用于在订单查看的时候
     *
     * @param id 地址的唯一Id
     * @return 用户的地址信息
     */
    @GetMapping("addressId")
    public Result getAddressById(@RequestParam("id") String id) {
        return addressService.findByIdAddress(id);

    }

    /**
     * 用户抓取记录
     *
     * @param userId 用户的唯一ID
     * @return 用户抓取记录
     */
    @GetMapping("grabNote")
    public Result listGrabNote(@RequestParam("userId") String userId, boolean status, Integer page, Integer pageSize) {
        //默认第一页
        if (page == null || page < 1) {
            page = 1;
        }
        //默认页面大小为10
        if (pageSize == null || pageSize < 0 || pageSize > 100) {
            pageSize = 10;
        }
        Map<String, Object> map = grabNoteService.listGrabNoteService(userId, status, page, pageSize);
        return success(map.get("result"));
    }

    /**
     * 获取指定用户指定抓取状态的数量的总页数
     *
     * @param userId 用户Id
     * @param status 状态
     * @return 返回结果
     */
    @GetMapping("pages")
    public Result grabNotePageSize(@RequestParam("userId") String userId, boolean status, Integer pageSize) {
        //默认页面大小为10
        if (pageSize == null || pageSize < 0 || pageSize > 100) {
            pageSize = 10;
        }
        Map<String, Object> map = grabNoteService.listGrabNoteService(userId, status, 1, pageSize);
        return success(map.get("total"));
    }


    /**
     * 签到记录
     *
     * @param userId 用户的唯一ID
     * @return 返回签到记录
     */
    @GetMapping("sign")
    public Result listSign(@RequestParam("userId") String userId) {
        return signService.listAllSignService(userId);
    }

    /**
     * 用户订单查询
     *
     * @param userId 用户的唯一ID
     * @return 用户订单查询
     */
    @GetMapping("order")
    public Result listOrder(@RequestParam("userId") String userId) {
        return success(orderClientService.listOrderService(userId));
    }

    /**
     * 积分使用情况
     *
     * @param userId 用户的唯一ID
     * @return 返回积分使用情况
     */
    @GetMapping("score")
    public Result listScore(@RequestParam("userId") String userId) {
//        todo  待实现
        System.out.println(userId);
        return success();
    }

    /**
     * 充值记录
     *
     * @param userId 用户的唯一ID
     * @return 返回充值记录信息
     */
    @GetMapping("recharge")
    public Result listRecharge(@RequestParam("userId") String userId) {
        return success(orderClientService.listRechargeService(userId));
    }

    /**
     * 通过邀请码查询全部的填写了该邀请码的信息
     *
     * @param code 邀请码
     * @return 返回填写了该邀请码的人的信息列表
     */
    @GetMapping("invitation")
    public Result listInvitation(@RequestParam("code") String code) {
        return invitationService.listInvitationService(code);
    }

}
